import { asyncRoutes, constantRoutes } from '@/router'
import store from '@/store'

/**
 * Use meta.role to determine if the current user has permission
 * @param roles
 * @param route
 */
// function hasPermission(resources, route) {
//   resources.forEach(route => {
//     const tmp = { ...route }
//     console.log(route.path, tmp.path)
//     if (route.path == tmp.path) {

//       if (tmp.children) {
//         tmp.children = hasPermission(tmp.children, roles)
//       }
//       // res.push(tmp)
//       return true
//     }
//   })
// if (route.meta && route.meta.roles) {
//   return roles.some(roles => route.meta.roles.includes(roles))
// } else {
//   return true
// }
// }

/**
 * Filter asynchronous 通过递归过滤异步路由表
 * @param routes asyncRoutes
 * @param roles
 */
export function filterAsyncRoutes(resources) {
  let routes = asyncRoutes.concat([])
  let rolesRoute = []

  routes.forEach(route => {
    let arr = {}
    if (route.hidden) {
      // 判断path相同则 添加一级路由
      arr = route
    } else {
      resources.forEach(resRoute => {
        if (route.path === resRoute.path || true) {
          // 判断path相同则 添加一级路由
          arr = route
          // 再循环找匹配的二级路由，设置children
          arr.children = filterAsyncRoutesChild(route.children, resRoute)
        }
      })
    }


    // 当arr值不为空的默认值时候，再追加到数组
    if (Object.keys(arr).length > 0) {
      rolesRoute.push(arr)
    }
  })
  return rolesRoute
}

// 过滤路由的 chidlren 循环找 拥有的二级路由权限
export function filterAsyncRoutesChild(routes, resRoute) {
  let rolesRouteChild = []
  routes.forEach(route => {
    let arr = {}
    if (route.hidden) {
      // 隐藏的下级路由默认加上，暂不做判断
      arr = route
    } else {
      // 其他显示的路由，在返回的列表找，找到再加进来
      for (let j = 0; j < resRoute.children.length; j++) {
        let resRouteChild = resRoute.children[j]
        if (route.path === resRouteChild.path || true) {
          arr = route
        }
      }
    }

    if (Object.keys(arr).length > 0) {
      rolesRouteChild.push(arr)
    }
  })
  return rolesRouteChild
}

const state = {
  routes: [],
  addRoutes: []
}

const mutations = {
  SET_ROUTES: (state, routes) => {
    state.addRoutes = routes
    state.routes = constantRoutes.concat(routes)
  }
}

const actions = {
  generateRoutes({ commit }, roles) {
    return new Promise(resolve => {
      const resources = store.getters.info.userInfo.resources
      console.log(resources)
      let accessedRoutes
      // accessedRoutes = filterAsyncRoutes(resources)
      if (roles == 1) {
        // accessedRoutes = asyncRoutes || []
        accessedRoutes = filterAsyncRoutes(resources)
        for (let i = 0; i < accessedRoutes.length; i++) {
          let item = accessedRoutes[i]
          if (item.path === '/member-manage') {
            for (let j = 0; j < item.children.length; j++) {
              let itemChild = item.children[j]
              if (itemChild.path === 'rebate' || itemChild.path === 'set-rebate') {
                item.children.splice(j, 1)
              }
            }
          }
        }
      } else {
        accessedRoutes = filterAsyncRoutes(resources)
      }

      commit('SET_ROUTES', accessedRoutes)
      resolve(accessedRoutes)
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
